<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入证书名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['cert:cert_type:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cert_typeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="证书类别" align="center" prop="type"/>
      <el-table-column label="证书名称" align="center" prop="name"/>
<!--      <el-table-column label="证书模板ID" align="center" prop="tplId"/>-->
<!--      <el-table-column label="有效期(月)" align="center" prop="validMonth"/>-->
      <el-table-column label="说明" align="center" prop="info"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['cert:cert_type:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['cert:cert_type:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改证书类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="证书类别" prop="type">
          <el-input v-model="form.type" placeholder="请输入证书类别" style="width: 250px"/>
        </el-form-item>
        <el-form-item label="证书名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入证书名称" style="width: 250px"/>
        </el-form-item>
<!--        <el-form-item label="证书模板" prop="tplId">-->
<!--          <el-input v-model="form.tplId" placeholder="请输入证书模板" style="width: 250px"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="有效期(月)" prop="validMonth">-->
<!--          <el-input-number v-model="form.validMonth" controls-position="right" :min="0"/>-->
<!--        </el-form-item>-->
        <el-form-item label="说明" prop="info">
          <el-input v-model="form.info" placeholder="请输入说明" class="input-w-300" type="textarea" :rows="5"/>
        </el-form-item>
<!--        <el-form-item label="编号规则" prop="codeRule">-->
<!--          <el-input v-model="form.codeRule" type="textarea" :rows="5" style="width: 300px" placeholder="请输入编号规则"/>-->
<!--        </el-form-item>-->
        <!-- <el-form-item label="编号规则" prop="codeRule">
            <el-button type="primary" @click="addList" size="mini">添加一行</el-button><br>
            <template v-for="(item,index) in flagList">
                <el-select v-model="item.option" placeholder="请选择" style="width: 150px;" @change="changeList($event,index)">
                    <el-option v-for="item in options"  :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
                <template v-if="item.data != undefined">
                    内容：<el-input  v-model="item.data" :readonly="item.readonly" style="width: 150px;" placeholder="请输入内容"></el-input>
                </template>
                <el-button type="danger" @click="deleteList(index)" icon="el-icon-delete" size="mini" circle></el-button><br>
            </template>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCert_type, getCert_type, delCert_type, addCert_type, updateCert_type} from "@/api/cert/cert_type";

export default {
  name: "Cert_type",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 证书类型表格数据
      cert_typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        tplId: null,
        validMonth: null,
        info: null,
        status: null,
        codeRule: null,
        reservedOne: null,
        reservedTwo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          {required: true, message: "证书类别不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "证书名称不能为空", trigger: "blur"}
        ],
        validMonth: [
          {required: true, message: "有效期不能为空", trigger: "blur"}
        ],
      },
      flagList: [
        {option: 1, data: null}
      ],
      options: [
        {value: '0', label: '自定义'},
        {value: '1', label: '当前年份'},
        {value: '2', label: '当前年+月'},
        {value: '3', label: '当前年+月+日'},
        {value: '4', label: '流水号（字母，1位）'},
        {value: '5', label: '流水号数字'},
        {value: '6', label: '用户编码'},
        {value: '7', label: '部门编码'},
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeList(e, index) {
      console.log(e);
      this.flagList[index].data = '';
      if (e == '1') {
        this.flagList[index].readonly = true;
      } else {
        this.flagList[index].readonly = false;
      }


    },
    deleteList(index) {
      if (index == 0) {
        this.$modal.msgError("至少一行吧");
        return;
      }
      this.$delete(this.flagList, index);
    },
    addList() {
      this.flagList.push({option: 1, data: null})
    },
    /** 查询证书类型列表 */
    getList() {
      this.loading = true;
      listCert_type(this.queryParams).then(response => {
        this.cert_typeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        tplId: null,
        validMonth: null,
        info: null,
        status: null,
        codeRule: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        reservedOne: null,
        reservedTwo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加证书类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCert_type(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改证书类型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCert_type(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCert_type(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除证书类型编号为"' + ids + '"的数据项？').then(function () {
        return delCert_type(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cert/cert_type/export', {
        ...this.queryParams
      }, `cert_type_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
